package com.wanglibao.woodtrade.woodtrade.ui.fragment.adapter;

import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.wanglibao.woodtrade.woodtrade.R;
import com.wanglibao.woodtrade.woodtrade.ui.fragment.adapter.model.Commodity;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by lugg on 2016/5/10.
 */
public class HomeFragmentRecommendAdapter extends BaseAdapter {

    Context context;
    Commodity commodity;
    boolean isShowHot = true;
    public HomeFragmentRecommendAdapter(Context context,Commodity commodity){
        this.context = context;
        this.commodity = commodity;
    }

    public void setIsShowHotNo(){
        this.isShowHot = false;
    }

    @Override
    public int getCount() {
        return commodity.getList().size();
    }

    @Override
    public Object getItem(int position) {
        return commodity.getList().get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null){
            convertView = View.inflate(context,R.layout.item_lv_fragment_home_recommed,null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }
        Commodity.ListBean listBean = commodity.getList().get(position);

        if (!TextUtils.isEmpty(listBean.getDefault_img())){
            viewHolder.sdv_list_item_fragment_home_recommed_image.setImageURI(Uri.parse(listBean.getDefault_img()));
        }
        viewHolder.tv_list_item_fragment_home_recommed_name.setText(listBean.getTitle());
        viewHolder.tv_list_item_fragment_home_recommed_price.setText("¥"+listBean.getPrice()+"元/"+listBean.getUnit_name());
        viewHolder.tv_list_item_fragment_home_recommed_specifications.setText("规格: "+listBean.getSize());
        viewHolder.tv_list_item_fragment_home_recommed_address.setText(listBean.getArea());
        viewHolder.tv_list_item_fragment_home_recommed_goods_click.setText("热度: "+listBean.getGoods_click());
        if (!isShowHot){
            viewHolder.tv_list_item_fragment_home_recommed_goods_click.setVisibility(View.GONE);
        }
        return convertView;
    }
    static class ViewHolder{
        public ViewHolder(View view){
            ButterKnife.bind(this,view);
        }

        @Bind(R.id.sdv_list_item_fragment_home_recommed_image)
        SimpleDraweeView sdv_list_item_fragment_home_recommed_image;

        @Bind(R.id.tv_list_item_fragment_home_recommed_name)
        TextView tv_list_item_fragment_home_recommed_name;

        @Bind(R.id.tv_list_item_fragment_home_recommed_specifications)
        TextView tv_list_item_fragment_home_recommed_specifications;

        @Bind(R.id.tv_list_item_fragment_home_recommed_price)
        TextView tv_list_item_fragment_home_recommed_price;

        @Bind(R.id.tv_list_item_fragment_home_recommed_address)
        TextView tv_list_item_fragment_home_recommed_address;

        @Bind(R.id.tv_list_item_fragment_home_recommed_goods_click)
        TextView tv_list_item_fragment_home_recommed_goods_click;
    }

    public void update(Commodity commodity){
        this.commodity = commodity;
        super.notifyDataSetChanged();
    }
}
